<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Khenthuongcanbophong Model
 */
class VastModelKhenthuongcanbophong extends JModelForm {

    /**
     * @var object item
     */
    protected $_item;

    /**
     * Get the khenthuongcanbophong
     * @return object The khenthuongcanbophong to be displayed to the user
     */
    function getItem() {

        if (!isset($this->_item)) {
            $id = JRequest::getInt('khenthuongid');
            
            $db = JFactory::getDBO();
            $query = $db->getQuery(true);

            $query->select('a.*');
            $query->from('#__khenthuong AS a');
            $query->where('a.id = ' . $id);

            $db->setQuery($query);
            $khenthuong = $db->loadObject();
            
            $this->_item = array();
            if (count($khenthuong) > 0) foreach ($khenthuong as $key => $value) {
                $this->_item[$key] = $value;
            }
        }
        
        return $this->_item;
    }
    
    public function createItem($data) {
        // set the data into a query to update the record
        $db = $this->getDbo();
        
        $query = 'INSERT INTO #__khenthuong(`lydo`, `hinhthuc`, `capkhenthuong`, `ngayquyetdinh`, `trangthai`, `hosocanbo_id`, `nguoikiquyetdinh`)';
        $query .= ' VALUES(';
        $query .= $db->quote($data['lydo']) . ',' .
                  $db->quote($data['hinhthuc']) . ',' .
                  $db->quote($data['capkhenthuong']) . ',' .  
                  $db->quote($data['ngayquyetdinh']) . ',' .
                  (int)($data['trangthai']) . ',' .
                  (int)($data['hosocanbo_id']) . ',' .
                  $db->quote($data['nguoikiquyetdinh']) . ')';                         
        
        $db->setQuery($query);
                                
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            $query = $db->getQuery(true);
            $query->clear();
            
            $query->select('a.*');
            $query->from('#__hosocanbo AS a');
            $query->where('a.id = ' . $data['hosocanbo_id']);
            
            $db->setQuery($query);
            $canbo = $db->loadObject();
            
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Thêm thông tin khen thưởng của cán bộ ' . $canbo->hoten, JFactory::getUser()->id, VastHelper::getHosocanbo()->id);
            return true;
        }
    }

    public function updateItem($data) {
        $id = JRequest::getInt('khenthuongid');
        
        // set the data into a query to update the record
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->clear();
        $query->update('#__khenthuong');
        
        foreach($data as $key => $value) {
            if (is_numeric($value)) {
                $query->set($key . ' = ' . $value);
            } else {
                $query->set($key . ' = ' . $db->quote($value));
            }
        }
        
        $query->where('id = ' . $id);
        
        $db->setQuery($query);
        
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            $query = $db->getQuery(true);
            $query->clear();
            
            $query->select('a.*');
            $query->from('#__hosocanbo AS a');
            $query->where('a.id = ' . $data['hosocanbo_id']);
            
            $db->setQuery($query);
            $canbo = $db->loadObject();
            
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Cập nhật thông tin khen thưởng của cán bộ ' . $canbo->hoten, JFactory::getUser()->id, VastHelper::getHosocanbo()->id);
            return true;
        }
    }
    
    public function deleteItem($khenthuongid)
    {        
        // set the data into a query to update the record
        $db = $this->getDbo();

        $query = 'DELETE FROM #__khenthuong WHERE `id` = ' . (int)$khenthuongid;
                
        $db->setQuery($query);
        
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            $query = $db->getQuery(true);
            $query->clear();
            
            $query->select('a.*');
            $query->from('#__hosocanbo AS a');
            $query->join('INNER', '#__khenthuong AS b ON b.hosocanbo_id = a.id');
            $query->where('b.id = ' . $id);
            
            $db->setQuery($query);
            $canbo = $db->loadObject();
            
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Xóa thông tin khen thưởng của cán bộ ' . $canbo->hoten, JFactory::getUser()->id, VastHelper::getHosocanbo()->id);
            return true;
        }        
    }
    
    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_vast.khenthuongcanbophong', 'khenthuongcanbophong', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id')) {
            $id = $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else {
            $id = $jinput->get('id', 0);
        }

        return $form;
    }

    protected function loadFormData()
    {
        $data = $this->getItem();

        if (empty($data))
        {
            $data = array();
        }
                        
        return $data;
    }
    
    public function getCanbos()
    {
        $id = JRequest::getInt('phongid');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('c.*');
        $query->from('#__phong AS a');
        $query->join('INNER', '#__congtac AS b ON b.phong_id = a.id');
        $query->join('INNER', '#__hosocanbo AS c ON c.id = b.hosocanbo_id');
        $query->where('a.id = ' . $id);
        $query->where('b.taivi = 1');
        
        $db->setQuery($query);
        $phong = $db->loadObjectList();
        
        return $phong;
        
    }   
     
    public function getPhong()
    {
        $id = JRequest::getInt('phongid');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__phong AS a');
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        $phong = $db->loadObject();
        
        return $phong;
    }

    public function getKhoi() 
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $donvi = $this->getDonvi();
        $phong = $this->getPhong();
        if ($donvi !== NULL)
        {
            $query->select('a.*');
            $query->from('#__khoi AS a');
            $query->where('a.id = ' . $donvi->khoi_id);
            $db->setQuery($query);
            
            return $db->loadObject();            
        }
        elseif ($phong !== NULL)
        {
            $query->select('a.*');
            $query->from('#__donvi AS a');
            $query->where('a.id = ' . $phong->donvi_id);
            $db->setQuery($query);            
            
            $tmp = $db->loadObject();
            if ($tmp !== NULL)
            {
                $query->select('a.*');
                $query->from('#__khoi AS a');
                $query->where('a.id = ' . $tmp->khoi_id);
                $db->setQuery($query);
                
                return $db->loadObject();                            
            }
        }
        
        return NULL;
    }
        
    public function getDonvi() {
        $id = JRequest::getInt('phongid');
      
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->join('INNER', '#__phong AS b ON b.donvi_id = a.id');
        $query->where('a.id = ' . $id);
        $db->setQuery($query);
        return $db->loadObject();

    }      
    
}